Java OOPS II Inheritance (Part -1 ) II Core Java II Selenium with Java II Automation
Автор: Knowledge Share
Загружено: 2020-09-30
Просмотров: 81
Описание:
In this session you will learn what you learn how to create class using Inheritance with examples.
OOPS Inheritance
=========================
Definition :
Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another
Sub class acquires the properties and behaviours of Super class
Types of Inheritance
====================
1. Single Inheritance
2. Multi-level Inheritance
3. Hierarchical Inheritance
4. Multiple Inhertance ( not supported by java, can be achieved using interface )
Why Inheritance is used :
To organize the data in hierarchical order
To avoid creating multiple objects
To save memory consumption
Keyword used : "extends"
==============
Example :
=====================
class super
{
}
class sub extends super
{
}
Single Inheritance
============================
package oops_Examples;
class myclassA
{
int a=10;
void method_A()
{
System.out.println("this is a method from Class A");
}
}
class myclassB extends myclassA
{
int b=5;
void method_B()
{
System.out.println("this is a method from Class B");
}
}
public class single_inhertance_Example
{
public static void main(String args[])
{
System.out.println("Single Inheritance Example");
myclassB obj=new myclassB();
obj.method_A();
obj.method_B();
System.out.println("Variable from Class A " + obj.a);
System.out.println("Variable from Class B " + obj.b);
}
}
Multi-level Inheritance
========================
package oops_Examples;
class myclassAA
{
int a=10;
void method_AA()
{
System.out.println("this is a method from Class A");
}
}
class myclassBB extends myclassA
{
int b=5;
void method_BB()
{
System.out.println("this is a method from Class B");
}
}
class myclassCC extends myclassBB
{
int c=7;
void method_CC()
{
System.out.println("this is a method from Class C");
}
}
public class multi_level_Inheritance_Example
{
public static void main(String args[])
{
System.out.println("multi level inheritance example ");
myclassCC obj=new myclassCC(); obj.method_A(); obj.method_BB();
obj.method_CC(); System.out.println("varaibla from classAA : " + obj.a);
System.out.println("varaibla from classBB : " + obj.b);
System.out.println("varaibla from classCC: " + obj.c);
// Object for myclassBB
/*
myclassBB obj =new myclassBB(); obj.method_A(); obj.method_BB();
*/
}
}
Hierarchical Inheritance Example :
==========================================
package oops_Examples;
class myclA
{
int a=1;
void method_A()
{
System.out.println("this is method from myclA");
}
}
class myclB extends myclA
{
int b=2;
void method_B()
{
System.out.println("this is method from myclB");
}
}
class myclC extends myclA
{
int c=3;
void method_C()
{
System.out.println("this is method from myclC");
}
}
public class hierarchical_Inheritance_Example
{
public static void main(String args[])
{
System.out.println("multi-level Inhertance example");
myclC obj=new myclC();
obj.method_A();
obj.method_C();
System.out.println("variable of myclA : " + obj.a);
System.out.println("variable of myclC : " + obj.c);
/*
myclB obj=new myclB();
*/
}
}
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: