CoreJava Day 5 ( Class Object Inheritance)
CoreJava Day 5 ( Class Object Inheritance)
- HemanthKumar C
Day 5
Java Oops Concepts
Class and Object
Class is a blueprint or a template to create object .
Object is real time entity which has its own state and behavior
State : state defines the non – static variables , what data it can hold.
Behaviour : behavior defines non – static methods and the way it can behave.
➢ Whenever we create an object we get both state and behavior.
➢ The object address will be stored in reference variable.
➢ Multiple reference variable can hold multiple object address.
Ref1 address object Ref2 address object
➢ Any changes made to the object through a reference variable, it will not affect other reference variables.
3. hierarchical Inheritance }
Tester ©
Int : x
Sample © Demo ©
Tester 4 ©
Int : x
Demo 4 ©
Sample 6 ©
Void : add ( )
Int : x
Sample 4 ©
Void : disp ( )
METHOD OVERRIDING
Ex: class WhatsApp _v1 {
Developing a method in the sub class with the same
name and signature as in the superclass but with different
implementation in the sub class is called as method Void status ( ) {
overriding.
System.out.println ( “status with text” ) ;
Rules : }}
1.The method name and signature should be same in the
sub class as in the super class. class WhatsApp _ v2 extends WhatsApp _ v1 { Void
2.There should be IS A RELATIONSHIP ( inheritance ). status ( ) {
3.The method be non – static. System.out.println ( “status with text , images ,videos ” ) ;
}}
class Mainclass {
Public static void main ( String [ ] args ) {
WhatsApp w2 = new WhatsApp _ v2 ( ) ; W2 .
status ( ) ;
}}
SUPER KEYWORD
Super keyword is used in the case of method overriding , along with the sub class implementation , if we need super
class implementation and sub class implementaion thus we should go for super . method name
Note :
➢ We go for inheritance for code reusability .
➢ We go for method overriding whenever we want to provide new implementation for the old feature .
➢ We go for method overloading whenever we want to perform common task
and operation .
1. int x = (int) 59.9d; s.o.p(x); → o/p – 59 2. Short y = (short) 36.61; s.o.p(y); → o/p – 36
3. byte z = (byte) 99.9f; s.o.p(z); → o/p – 99 4. Long t = (long) 60.36; s.o.p(y); → o/p – 60
Note :
Sample s1 = new Sample ( ) ; // homogenous type of object creation. Sample s1 = new Demo ( ) ; // heterogenous type of object creation.
Int a Super class object
Super class
type
( up casting ) ( Down casting )
Sample ©
Void : disp ( )
Sub class type
Sub class object
Ex : Class Demo {
Int a=10;
}
Class Sample extends Demo {
Void disp ( ) {